package com.onpositive.wikitemplate;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;

import com.onpositive.wikitemplate.CountryInfo_ISO_3166_1_alpha_3.CountryInfo;

public class DefaultTemplateResolverProvider extends AbstractTemplateResolverProvider {
	
	public static DefaultTemplateResolverProvider getInstance(){
		
		if( instance == null )
			instance = new DefaultTemplateResolverProvider() ;
		
		return instance;
	}
	
	static private DefaultTemplateResolverProvider instance ;
	private DefaultTemplateResolverProvider(){
	}
	
	@Override
	public BasicTemplateResolver getResolver( String templateName )
	{
		return resolverMap.get( templateName.toLowerCase() ) ;
	}
	
	private HashMap<String,BasicTemplateResolver> resolverMap= new HashMap<String, BasicTemplateResolver>() ;
	{
		resolverMap.put( "convert", new Resolver_convert() ) ;
		resolverMap.put( "gaps"  , new Resolver_gaps() ) ;
		resolverMap.put( "nowrap", new Resolver_nowrap() ) ;
		
		final Resolver_as_of resolver_as_of = new Resolver_as_of();
		resolverMap.put( "as of",  resolver_as_of ) ;
		resolverMap.put( "as_of",  resolver_as_of ) ;
		
		final Resolver_killAttributes resolver_killAttributes = new Resolver_killAttributes();
		resolverMap.put( "nts", resolver_killAttributes ) ;
		
		final Resolver_killAttributes_inverse resolver_killAttributes_inverse = new Resolver_killAttributes_inverse();
		resolverMap.put( "dts", resolver_killAttributes_inverse ) ;		
		
		resolverMap.put( "flag icon", new Resolver_flagcountry() ) ;
		resolverMap.put( "flag country", new Resolver_flagcountry() ) ;
		resolverMap.put( "flagicon", new Resolver_flagcountry() ) ;
		resolverMap.put( "flagcountry", new Resolver_flagcountry() ) ;
		resolverMap.put( "flag", new Resolver_flagcountry() ) ;
	}
	
	private static final class Resolver_convert extends BasicTemplateResolver{		

		@Override
		public String resolveValue(String str, String oldStr) {
			
			ArrayList<String> arr = split(str);				
			int size = arr.size() ;
			if( size == 0 )
				return "" ;			
						
			int l = Math.min( size, 2 ) ;		
			
			StringBuilder builder = new StringBuilder() ;
			builder.append( arr.get(0) ) ;
			for( int i = 1 ; i < l ; i++ ){
				builder.append( " " ) ;
				builder.append( arr.get(i) ) ;				
			}
			
			String result = builder.toString() ;
			return result ;
		}
	}
	
	private static final class Resolver_gaps extends BasicTemplateResolver{
		
		@Override
		public String resolveValue(String str, String oldStr) {
			
			ArrayList<String> arr = split(str);				
			int size = arr.size() ;
			StringBuilder builder = new StringBuilder() ;
	
			for( String s : arr ){
				if( !Character.isDigit(s.charAt(0))  )
					builder.append( " " ) ;
			
				builder.append( s ) ;						
			}
			
			String result = builder.toString() ;
			return result ;
		}
	}
	
	private static final class Resolver_nowrap extends BasicTemplateResolver{
		
		@Override
		public String resolveValue(String str, String oldStr) {
			
			ArrayList<String> arr = split(str);				
			int size = arr.size() ;
			if( size == 0 )
				return "" ;
			
			StringBuilder builder = new StringBuilder() ;
	
			for( String s : arr ){
				builder.append( s ) ;
				builder.append( " " ) ;	
			}
			final String result = builder.substring(0, builder.length()-1) ;
			return result ;
		}
	}
	
	private static final class Resolver_as_of extends BasicTemplateResolver{
		
		@Override
		public String resolveValue(String str, String oldStr) {
			
			ArrayList<String> arr = split(str);				
			int size = arr.size() ;
			if( size == 0 )
				return "" ;
			
			StringBuilder builder = new StringBuilder() ;	

			builder.append( "(" ) ;
			int start = 0 ;
			for( ; start < size && arr.get(start).length() > 0 && !Character.isDigit( arr.get(start).charAt(0)); start++ );
			String year = arr.get( start + 0) ;
			String monthStr = size > start+1 ? arr.get(start + 1) : null ;
			String day = size > start+2 ? arr.get(start+2) : null ;
				
			if( monthStr!=null ){
				try{
					int monthInt = Integer.parseInt(monthStr) ;
					GregorianCalendar cl = new GregorianCalendar() ;
					cl.set( GregorianCalendar.MONTH, monthInt ) ;
					String month = cl.getDisplayName( GregorianCalendar.MONTH, GregorianCalendar.LONG , Locale.US ) ; 
					builder.append(month+" ") ;
						
					if( day!=null )
						builder.append(day+" ") ;
				}
				catch(NumberFormatException e)
				{
						
				}				
			}			
				
			builder.append(year) ;
			builder.append( ")" ) ;
			return builder.toString();
		}
	}
	
	private static final class Resolver_killAttributes extends BasicTemplateResolver{		

		@Override
		public String resolveValue(String str, String oldStr) {
			
			ArrayList<String> arr = split(str);				
			int size = arr.size() ;
			if( size == 0 )
				return "" ;			
			
			StringBuilder builder = new StringBuilder() ;
			for( String s : arr ){
				if( s.contains("=") )
					continue ;
				
				builder.append( s ) ;
				builder.append( " " ) ;	
			}
			final String result = builder.substring(0, builder.length()-1) ;
			return result ;
		}
	}
	
	private static final class Resolver_killAttributes_inverse extends BasicTemplateResolver{		

		@Override
		public String resolveValue(String str, String oldStr) {
			
			ArrayList<String> arr = split(str);				
			int size = arr.size() ;
			if( size == 0 )
				return "" ;			
			
			StringBuilder builder = new StringBuilder() ;
			for( int i = arr.size() ; i > 0 ; i-- )
			{
				String s = arr.get(i-1) ;
				if( s.contains("=") )
					continue ;
				
				builder.append( s ) ;
				builder.append( " " ) ;	
			}
			final String result = builder.substring(0, builder.length()-1) ;
			return result ;
		}
	}
	
	private static final class Resolver_flagcountry extends BasicTemplateResolver{		

		@Override
		public String resolveValue(String str, String oldStr) {
			
			int ind = str.indexOf("|") ;
			if(ind>=0)
				str = str.substring(0, ind) ;
			
			if( str.length() == 3 && str.toUpperCase().equals(str) ) {
				
				String result = DefaultNoParamsTemplateResolver.getinstance().resolve(str) ;
				if( result == null )
					return result ;				
			}
			
			String result = "[["+str+"]]" ;
			return result ;
		}
	}	

	private static ArrayList<String> split(String str) {
		ArrayList<String> arr = new ArrayList<String>() ;
		int prev = 0 ; 
		for( int i = str.indexOf("|") ; i >= 0 ; i = str.indexOf("|",prev) )
		{
			String s = str.substring(prev, i) ;
			if( s.length() != 0 )
				arr.add(s.trim()) ;
			
			prev = i+1 ;						
		}
		if( prev < str.length() )
			arr.add(str.substring(prev).trim()) ;
		return arr;
	}
	
	
}
